#import "HomeListSearchViewController.h"
#import "UserListInfo.h"
#import "HomeListSerachCell.h"
#import "SearchTextField.h"
#import "OmUserPageVC.h"
@interface HomeListSearchViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSMutableArray<UserListInfo *>* listArray;
@property (nonatomic, strong) UserListInfo* listModel;
@end
static NSString * const homeListSerachCellId = @"homeListSerachCellId";
@implementation HomeListSearchViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBaseInfo];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)setBaseInfo{
    [super setBaseInfo];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 5, 0, 0);
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeListSerachCell class]) bundle:nil] forCellReuseIdentifier:homeListSerachCellId];
}
- (void)getListSearchActorNameData{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"request_search"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:SafeStr(self.keyWord) forKey:@"key_word"];
    [CYNET POSTv3:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        _listArray = [NSMutableArray arrayWithArray:[NSArray modelArrayWithClass:[UserListInfo class] json:[responseObject valueForKey:@"list"]]];
        [self.tableView reloadData];
    } failure:^(NSString *error, NSInteger code) {
        [[HUDHelper sharedInstance] tipMessage:error];
    } hasCache:NO];
}
#pragma mark - UITableViewDelegate/dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeListSerachCell * cell = [tableView dequeueReusableCellWithIdentifier:homeListSerachCellId forIndexPath:indexPath];
    UserListInfo* model = self.listArray[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* view = [[UIView alloc] init];
    view.backgroundColor = [UIColor qmui_colorWithHexString:@"#eeeeee"];
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OmUserPageVC *vc = [[OmUserPageVC alloc] init];;
    UserListInfo* model = self.listArray[indexPath.section];
    vc.ID = model.id;
    [self.navigationController pushViewController:vc animated:NO];
}
#pragma mark - textField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    self.keyWord = textField.text;
    [textField resignFirstResponder];
    if (self.keyWord.length != 0) {
        [self getAllSearchDataWithSearchText:self.keyWord];
    }else{
        [self.listArray removeAllObjects];
        [self.tableView reloadData];
    }
    return YES;
}
- (void)textFieldEditChanged:(UITextField *)textField{
    self.keyWord = textField.text;
    if (self.keyWord.length != 0) {
        [self getAllSearchDataWithSearchText:self.keyWord];
    }else{
        [self.listArray removeAllObjects];
        [self.tableView reloadData];
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.searchText resignFirstResponder];
}
-(void)getAllSearchDataWithSearchText:(NSString *)keyword{
    [self getListSearchActorNameData];
}
#pragma mark - 懒加载
-(NSMutableArray<UserListInfo *> *)listArray{
    if (!_listArray) {
        _listArray = @[].mutableCopy;
    }
    return _listArray;
}
@end
